/**
 * 数学常用方法
 */
export default class MyMath {
	static map(v, a, b, c, d) {
		return c + ((d - c) * (v - a)) / (b - a);
	}

	static lerp(a, b, t) {
		return (b - a) * t + a;
	}

	static normalize(v, a, b) {
		return (v - a) / (b - a);
	}

	static clamp(v, min, max) {
		return Math.max(min, Math.min(max, v));
	}

	static cmap(v, a, b, c, d) {
		return lerp(c, d, cnormalize(v, a, b));
	}

	static cnormalize(v, a, b) {
		return clamp(normalize(v, a, b), 0, 1);
	}

	static clerp(a, b, t) {
		return clamp(lerp(a, b, t));
	}

	static between(min, max) {
		return function(v) {
			return min <= v && v < max;
		};
	}

	static random(start, end) {
		return Math.random() * (end - start) + start;
	}

	static hashCode(str) {
		var h = 0;
		var len = str.length;
		var t = 2147483648;
		for (var i = 0; i < len; i++) {
			h = 31 * h + str.charCodeAt(i);
			if (h > 2147483647) h %= t; //java int溢出则取模
		}
		return h;
	}
}
